package com.softWang.system.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.softWang.common.base.BaseServiceMy;
import com.softWang.common.exception.LogicException;
import com.softWang.system.controller.vo.systemDictData.*;
import com.softWang.system.mapper.SystemDictDataMapper;
import com.softWang.system.mapper.SystemDictTypeMapper;
import com.softWang.system.po.SystemDictData;
import com.softWang.system.po.SystemDictType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SystemDictDataService extends BaseServiceMy<SystemDictData> {
    @Resource
    private SystemDictDataMapper systemDictDataMapper;
    @Resource
    private SystemDictTypeMapper systemDictTypeMapper;

    /**
     * 分页条件查询
     * @param param
     * @return
     */
    public List<SystemDictDataQueryAllResult> queryall(SystemDictDataQueryAllParam param){
        param.startPage();
        List<SystemDictDataQueryAllResult> results=systemDictDataMapper.queryall(param);
        return results;
    }

    /**
     * 插入字典数据
     * @param dictData 字典数据信息
     * @return 字典数据id
     */
    @Transactional
    public Long add(SystemDictData dictData) {
        // 验证参数信息
        valdation(dictData);

        SystemDictType dictType = systemDictTypeMapper.selectById(dictData.getDictTypeId());

        // 设置参数信息
        dictData.setDictKey(dictType.getDictKey());
        dictData.setState(SystemDictData.State.NORMAL.getCode());

        systemDictDataMapper.insert(dictData);

        return dictData.getId();
    }


    /**
     * 修改字典数据
     * @param dictData  字典数据信息
     * @return
     */
    @Transactional
    public Long edit(SystemDictData dictData) {
        // 验证参数信息
        valdation(dictData);

        SystemDictType dictType = systemDictTypeMapper.selectById(dictData.getDictTypeId());

        // 设置参数信息
        dictData.setDictKey(dictType.getDictKey());

        systemDictDataMapper.updateById(dictData);

        return dictData.getId();
    }

    /**
     * 获取详情
     * @param id id
     * @return 字典数据信息
     */
    public SystemDictData getById(Long id) {
        return systemDictDataMapper.selectById(id);
    }

    /**
     * 删除字典数据
     * @param ids id列表
     * @return void
     */
    @Transactional
    public void del(List<Long> ids) {
        //删除
        systemDictDataMapper.deleteBatchIds(ids);
    }


    private void valdation(SystemDictData param){
        // 验证label唯一性
        Long num = systemDictDataMapper.selectCount(
                new LambdaQueryWrapper<SystemDictData>()
                        .eq(SystemDictData::getDictTypeId, param.getDictTypeId())
                        .eq(SystemDictData::getLabel, param.getLabel())
                        .ne(param.getId()!=null,SystemDictData::getId, param.getId()));
        if (num > 0) {
            throw new LogicException("标签已存在!");
        }
        // 验证value唯一性
        Long keyNum = systemDictDataMapper.selectCount(
                new LambdaQueryWrapper<SystemDictData>()
                        .eq(SystemDictData::getDictTypeId, param.getDictTypeId())
                        .eq(SystemDictData::getValue, param.getValue())
                        .ne(param.getId()!=null,SystemDictData::getId, param.getId()));
        if (keyNum > 0) {
            throw new LogicException("字典值已存在!");
        }
    }

    /**
     * 根据字典类型id获取字典数据列表
     * @param dictTypeId 字典类型id
     * @return 字典数据列表
     */
    public List<SystemDictDataItemResult> getListByDictTypeId(Long dictTypeId) {
        List<SystemDictDataItemResult> results = null;
        List<SystemDictData> dictDataList = systemDictDataMapper.selectList(
                new LambdaQueryWrapper<SystemDictData>()
                        .select(SystemDictData::getLabel, SystemDictData::getValue)
                        .eq(SystemDictData::getDictTypeId, dictTypeId)
                        .eq(SystemDictData::getState, SystemDictData.State.NORMAL.getCode())
        );
        if (dictDataList != null && dictDataList.size() > 0) {
            results = dictDataList.stream().map(item -> {
                SystemDictDataItemResult result = new SystemDictDataItemResult();
                result.setLabel(item.getLabel());
                result.setValue(item.getValue());
                return result;
            }).collect(Collectors.toList());
        }
        return results;
    }

    /**
     * 根据字典类型key获取字典数据列表
     * @param dictKey 字典类型key
     * @return 字典数据列表
     */
    public List<SystemDictDataItemResult> getListByDictKey(String dictKey) {
        List<SystemDictDataItemResult> results = new ArrayList<>();
        List<SystemDictData> dictDataList = systemDictDataMapper.selectList(
                new LambdaQueryWrapper<SystemDictData>()
                        .select(SystemDictData::getLabel, SystemDictData::getValue)
                        .eq(SystemDictData::getDictKey, dictKey)
                        .eq(SystemDictData::getState, SystemDictData.State.NORMAL.getCode())
        );
        if (dictDataList != null && dictDataList.size() > 0) {
            results = dictDataList.stream().map(item -> {
                SystemDictDataItemResult result = new SystemDictDataItemResult();
                result.setLabel(item.getLabel());
                result.setValue(item.getValue());
                return result;
            }).collect(Collectors.toList());
        }
        return results;
    }

    public String getLabelByTypeKeyAndVal(SystemDictDataGetLabelByTypeKeyAndValParam keyparam) {
        SystemDictData data = systemDictDataMapper.selectOne(new LambdaQueryWrapper<SystemDictData>()
                .eq(SystemDictData::getDictKey, keyparam.getKey())
                .eq(SystemDictData::getValue, keyparam.getValue()));
        if (data == null) {
            return null;
        }
        return data.getLabel();
    }

    public List<SystemDictAllDataResult> dictAllData() {
        List<SystemDictAllDataResult> results = systemDictDataMapper.dictAllData();
        return results;
    }
}
